//
//  LeftViewController.m
//  XDLookPic
//
//  Created by xiaorui on 14-4-17.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "LeftViewController.h"
#import "ClubCell.h"
#import "XRChooseLoveClubViewController.h"
#import "XRClubButton.h"


@interface LeftViewController ()

@end

@implementation LeftViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self acquireCulbModeData];
    
    mTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0,UI_SCREEN_WIDTH , UI_SCREEN_HEIGHT - UI_NAVIGATION_BAR_HEIGHT -UI_STATUS_BAR_HEIGHT)];
    mTableView.delegate =self;
    mTableView.dataSource =self;
    mTableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:mTableView];

}
#pragma mark  TheClubMode data
-(void)acquireCulbModeData{
    dataArray =[[NSMutableArray alloc]initWithCapacity:0];
    NSString *clubPath=[[NSBundle mainBundle]pathForResource:@"TheClubList" ofType:@"plist"];
    NSDictionary *clubDict=[NSDictionary dictionaryWithContentsOfFile:clubPath];
    TheClubModel *myselfClubMode=[[TheClubModel alloc]initWithTitle:@"我的俱乐部" subArray:@[@""]];
    myselfClubMode.open = YES;
    [dataArray addObject:myselfClubMode];
    NSArray *clubKeyArray =[clubDict allKeys];
    for (int i=0;i<clubKeyArray.count;i++) {
        
        TheClubModel *clubModel=[[TheClubModel alloc]initWithTitle:[clubKeyArray objectAtIndex:i] subArray:[clubDict objectForKey:[clubKeyArray objectAtIndex:i]]];
        clubModel.open =NO;
        [dataArray addObject:clubModel];
    }
}
#pragma mark tableView delegate and datasouce
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"ID";
    ClubCell *cell =[tableView dequeueReusableCellWithIdentifier:ID];
    TheClubModel *mode=[dataArray objectAtIndex:indexPath.row];
    if(!cell){
        cell =[[ClubCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.titleLabel.text =mode.title;
    }
    [cell.openButton addTarget:self action:@selector(openButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.openButton.tag =indexPath.row;
    if(mode.open ==YES){
        cell.itemView.hidden =NO;
        [cell.itemView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        for(int i =0;i<mode.subArray.count;i++){
            UIButton *bnt=[UIButton buttonWithType:UIButtonTypeContactAdd];
            bnt.frame =CGRectMake(5+(5 +35) *(i%3), 35*(i/3), 35, 35);
            [bnt setBackgroundImage:[UIImage imageNamed:@"Icon.png"] forState:UIControlStateNormal];
            [bnt addTarget:self action:@selector(clubItemClick:) forControlEvents:UIControlEventTouchUpInside];
            [cell.itemView addSubview:bnt];
        }
    }else{
        cell.itemView.hidden =YES;
    }
    return cell;
}
-(void)clubItemClick:(UIButton *)sender{
    XRChooseLoveClubViewController *loveClubVC=[[XRChooseLoveClubViewController alloc]init];
    DDLOG_CURRENT_METHOD;
    DDLOG(@"TTTTTTT");
    [self presentViewController:loveClubVC animated:YES completion:^{
    
    }];
    //    [self.navigationController pushViewController:loveClubVC animated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArray.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    TheClubModel *mode=[dataArray objectAtIndex:indexPath.row];
    if(mode.open == YES){
        if(mode.subArray.count%3 ==0){
            return (50+(mode.subArray.count/3)*35);

        }else{
            return (50+(mode.subArray.count/3 + 1)*35);
        }
    }else{
        return 50;
    }
}
-(void)openButtonClick:(UIButton *)sender{
    TheClubModel *mode=[dataArray objectAtIndex:sender.tag];
    
    if(mode.open == YES){
        mode.open =NO;
        
    }else{
        for(int i=0;i<dataArray.count;i++){
            TheClubModel *mode=[dataArray objectAtIndex:i];
            mode.open =NO;
        }
        mode.open =YES;
    }
    [mTableView reloadData];
}
#pragma mark  didRecivememoryWaring

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
